﻿/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

package
{
    import com.pblabs.box2D.*;
    import com.pblabs.box2D.Box2DDebugComponent;
    import com.pblabs.box2D.Box2DManagerComponent;
    import com.pblabs.box2D.Box2DSpatialComponent;
    import com.pblabs.box2D.CircleCollisionShape;
    import com.pblabs.box2D.PolygonCollisionShape;
    import com.pblabs.engine.core.*;
    import com.pblabs.engine.core.LevelManager;
    import com.pblabs.engine.entity.*;
    import com.pblabs.engine.entity.IEntity;
    import com.pblabs.engine.PBE;
    import com.pblabs.rendering2D.*;
    import com.pblabs.rendering2D.DisplayObjectScene;
    import com.pblabs.rendering2D.SpriteSheetRenderer;
    import com.pblabs.rendering2D.ui.*;
    import com.pblabs.rendering2D.ui.SceneView;
    import com.zog.Box2DTrigger;
    import com.zog.CarController;
    import com.zog.MarkerEvent;
    import com.zog.RacingLogic;
    import com.zog.SimplePlatform;
    import com.zog.StartAndFinishMarkerEvent;
    import flash.display.*;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.external.ExternalInterface;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import flash.system.Security;
    import flash.utils.*;
    
    [SWF(width="800", height="600", frameRate="60", backgroundColor="#08d4f8")]
    public class Demo extends Sprite
    {      
        public function Demo() 
        {
            Security.allowDomain("*");
            
            // Make sure all the types our XML will use are registered.
            PBE.registerType(flash.geom.Point);
            PBE.registerType(flash.geom.Matrix);
            PBE.registerType(flash.geom.Rectangle);
            PBE.registerType(flash.geom.Transform);

            PBE.registerType(com.pblabs.engine.PBE);
            PBE.registerType(com.pblabs.engine.core.LevelManager);
            
            PBE.registerType(com.pblabs.rendering2D.SpriteRenderer);
			PBE.registerType(com.pblabs.rendering2D.DisplayObjectScene);
            PBE.registerType(com.pblabs.rendering2D.SpriteSheetRenderer);
            PBE.registerType(com.pblabs.rendering2D.ui.SceneView);
            
            PBE.registerType(com.pblabs.box2D.Box2DDebugComponent);
            PBE.registerType(com.pblabs.box2D.Box2DManagerComponent);
            PBE.registerType(com.pblabs.box2D.Box2DSpatialComponent);
            PBE.registerType(com.pblabs.box2D.PolygonCollisionShape);
            PBE.registerType(com.pblabs.box2D.CircleCollisionShape);

            PBE.registerType(MarkerEvent);
            PBE.registerType(StartAndFinishMarkerEvent);
            PBE.registerType(RacingLogic);
            PBE.registerType(CarController);
			PBE.registerType(SimplePlatform);
			PBE.registerType(Box2DTrigger);

            if (this.stage)
            {
                onAddedToStage();
            } 
            else 
            {
                addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            }
        }

        private function createBox2DDebugView():void
        {
            if(PBE.nameManager.lookup("DEBUG") == null)
            {
                var e:IEntity = PBE.allocateEntity();
                e.initialize("DEBUG");

                var c:Box2DDebugComponent = new Box2DDebugComponent();
                c.spatialManager = PBE.spatialManager as Box2DManagerComponent;
                c.scene = PBE.scene;
                
                e.addComponent(c, "visual");
            }
        }
 
		private function onLevelLoaded(e:Event):void 
		{
            createBox2DDebugView();
		}        

        private function onAddedToStage(evt:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

			//Initialize the engine
            PBE.startup(this);
            
			// Load embedded resources.
            new Resources();
            
			var sceneView:SceneView = new SceneView();
			
            PBE.initializeScene( sceneView, "Scene", null, Box2DManagerComponent );
            
			PBE.scene.zoom = 0.6;
			PBE.scene.screenPan( -50, 40 );
			
			(PBE.spatialManager as Box2DManagerComponent).gravity = new Point(0, 0);
            
            PBE.levelManager.addEventListener(LevelEvent.LEVEL_LOADED_EVENT, onLevelLoaded);
            
			if(!ExternalInterface.available)
			{
				LevelManager.instance.load("../racing/levelDescriptions.xml", 1);
			}
		}
    }
}
